<?php namespace Manage\Http\Controllers;

use Manage\Http\Requests;
use Manage\Http\Controllers\Controller;

use Illuminate\Http\Request;

use Manage\Group,Manage\Base,Manage\Device,View,Input;

class GroupController extends Controller 
{

    /**
     * 小区管理
     * @param  id $id 指定小区
     * @return [type]        [description]
     */
    protected function arrange($id = 0)
    {
        //分组获取
        $groupData = Group::getGroup();
        Base::dPush('groupData', $groupData, $this);

        if( empty($id) )
        {
            return \View::make('group.arrange', $this->data);
        }

        //avtice操作
        Base::dPush('active', $id, $this);

        //分组修改
        if( Input::has('edit') )
        {
            $result = Group::changeGroup($id, Input::get('devices'));

            if( $result )
            {
                Base::dPush('error', 0, $this);
                Base::dPush('msg', '操作成功!', $this);
            }
            else
            {
                Base::dPush('error', 1, $this);
                Base::dPush('msg', '操作失败,请检查网络!', $this);
            }
        }


        //获取当前分组设备
        $groupDevs = Device::getAllDevicebyGroup($id);
        Base::dPush('groupDevs', json_encode($groupDevs), $this);

        //获取未分组设备
        $unGroupDevs = Device::getAllDeviceUngroup($id, 1);
        Base::dPush('unGroupDevs', $unGroupDevs, $this);

    	return \View::make('group.arrange', $this->data);
    }


    /**
     * 小区创建
     * @return [type]        [description]
     */
    protected function create()
    {

        if( Input::has('group-create') )
        {

            $gData = Input::all();
            array_pull($gData, 'group-create');

            $result = Group::createGroupDefault($gData);

            if( $result )
            {
                Base::dPush('error', 0, $this);
                Base::dPush('msg', '操作成功!', $this);
            }
            else
            {
                Base::dPush('error', 1, $this);
                Base::dPush('msg', '操作失败,请检查网络!', $this);
            }

        }

        //获取当前分组列表
        $groupList = Group::getGroupDefault();
        Base::dPush('groupList', $groupList, $this);

    	return \View::make('group.create', $this->data);
    }


    /**
     * 小区设定
     * @return [type]        [description]
     */
    protected function setting()
    {
        //分组获取
        $groupData = Group::getGroup();
        Base::dPush('groupList', $groupData, $this);

        //修改分组信息
        if ( Input::has('edit') )
        {
            $data = Input::all();
            $id = array_pull( $data, 'id' );
            array_pull($data, 'edit');
            Group::editGroupById($id, $data);
        }

        //传入选中分组
        if( Input::has('group') )
        {
            return Group::getGroupById(Input::get('group'));
        }

    	return \View::make('group.setting', $this->data);
    }

    /**
     * 小区详情
     * @param  int $id 小区ID
     * @return [type]     [description]
     */
    protected function info($id=0)
    {
        //获取小区设备
        if( empty($id) )
        {
            return;
        }
        
        return Group::getDevListByGroupId($id);
    }

}
